home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Icon Display / IconWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.1 KB  |  235 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        IconWindow.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "IconWindow.h"
  24. #include <Windows.h>
  25. #include <MacTypes.h>
  26. #include <Resources.h>
  27.  
  28. WindowPtr bullseyeWindow;
  29.  
  30. Rect        dragRect;
  31. Rect        windowBounds = { 40, 40, 300, 500 };
  32. Rect        circleStart = {10, 10, 100, 100};
  33. int            width = 5;
  34.  
  35. /****
  36.  * SetUpWindow()
  37.  *
  38.  *    Create the Bullseye window, and open it.
  39.  *
  40.  ****/
  41.  
  42. void SetUpWindow()
  43.  
  44. {
  45.     dragRect = qd.screenBits.bounds;
  46.     
  47.     bullseyeWindow = NewWindow(0L, &windowBounds, "\pBullseye", true, noGrowDocProc,(WindowPtr)-1L, true, 0);
  48.     SetPort(bullseyeWindow);
  49. }
  50. /* end SetUpWindow */
  51.  
  52. #define BYTESIZE 8
  53. #define WIDTHHEIGHT 16
  54.  
  55. GWorldPtr    GgwPtr = NULL;
  56. Rect    PlotRect = {10,200,26,216};
  57.  
  58. Boolean RectNotInGrayRgn(Rect *r)
  59. {
  60.     RgnHandle rgn = GetGrayRgn();
  61.     Rect    IRect,wRect;
  62.     
  63.     IRect = (**rgn).rgnBBox;
  64.     SectRect(&IRect,r,&wRect);
  65.     
  66.     return EmptyRect(&wRect);
  67. }
  68.  
  69. void FindNewDevice(WindowPtr    wind)
  70. {
  71.     Rect    gRect,lRect,wRect;
  72.     GDHandle    aDevice;
  73.     GDHandle    dominateDevice = NULL;
  74.     long        gArea,cArea;
  75.     GrafPtr        gp;
  76.     
  77.     GetPort(&gp);
  78.     SetPort(wind);
  79.     
  80.     gRect = PlotRect;
  81.     LocalToGlobal((Point *)&gRect.top);
  82.     LocalToGlobal((Point *)&gRect.bottom);
  83.     
  84.     aDevice = GetDeviceList();
  85.     gArea = cArea = 0;
  86.     while ( aDevice ) {
  87.         lRect = (**aDevice).gdRect;
  88.         SectRect(&lRect,&gRect,&wRect);
  89.         
  90.         if ( !EmptyRect(&wRect) ) {
  91.             cArea = (long)(wRect.right - wRect.left) * (long)(wRect.bottom - wRect.top);
  92.         
  93.             if ( cArea > gArea ) {
  94.                 gArea = cArea;
  95.                 dominateDevice = aDevice;
  96.             }
  97.         }
  98.         
  99.         aDevice = GetNextDevice(aDevice);
  100.     }
  101.     
  102.     if ( GgwPtr && dominateDevice ) {
  103.         if ( (**((**dominateDevice).gdPMap)).pixelSize != (**(GgwPtr->portPixMap)).pixelSize)
  104.             InvalRect(&PlotRect);
  105.     }
  106.     SetPort(gp);
  107. }
  108.         
  109.         
  110.     
  111.     
  112.  
  113. void MakeANewGWorld(Rect *r)
  114. {
  115.     Rect        localR;
  116.     Handle        h;
  117.     QDErr        err;
  118.     short        i;
  119.     Ptr            p,tp;
  120.     short        depth;
  121.     
  122.     
  123.     localR = *r;
  124.     
  125.     /* 
  126.        by passing a global rectangle we force NewGWorld to determine the resolution
  127.        of the device the the majority of the rectangle lies on.  Then we can use the pixel
  128.        depth to determine which icl or ics to get
  129.     */
  130.     LocalToGlobal((Point *)&localR.top);
  131.     LocalToGlobal((Point *)&localR.bottom);
  132.     
  133.     if ( RectNotInGrayRgn(&localR) )
  134.         return;
  135.     if ( !GgwPtr ) {
  136.         if ( err = NewGWorld(&GgwPtr,0,&localR,NULL,NULL,noNewDevice) )
  137.             return;
  138.     }
  139.     else
  140.     if ( err = UpdateGWorld(&GgwPtr,0,&localR,NULL,NULL,0) )
  141.            return;
  142.  
  143.         
  144.     /* now grab the right depth of icl or ics */
  145.         depth = (**(GgwPtr->portPixMap)).pixelSize; 
  146.         switch ( depth ) {
  147.              
  148.             case 2: SetRect(&localR,0,0,16,16);
  149.                     DisposeGWorld(GgwPtr);
  150.                     if ( err = NewGWorld(&GgwPtr,1,&localR,NULL,NULL,0) )
  151.                            return; 
  152.             case 1: h = GetResource('ics#',128);
  153.                     depth = 1;
  154.                     break;
  155.             case 4:    h = GetResource('ics4',128);
  156.                     break;
  157.             case 8:    h = GetResource('ics8',128);
  158.                     break;
  159.             default: h = GetResource('ics8',128);
  160.                      depth = 8;
  161.                      SetRect(&localR,0,0,16,16);
  162.                      DisposeGWorld(GgwPtr);
  163.                      if ( err = NewGWorld(&GgwPtr,8,&localR,NULL,NULL,0) )
  164.                            return; 
  165.                      break;
  166.         }
  167.         
  168.  
  169.         
  170.         
  171.         /* we will be working with the pixel data directly so lock them */
  172.         p = GetPixBaseAddr(GgwPtr->portPixMap);
  173.         if ( p ) {
  174.             HLock(h);
  175.             tp = *h;
  176.             
  177.             /* 
  178.                 because NewGWorld usually adds a long word of padding so that
  179.                 it can more easily keep the pixel map long word aligned we
  180.                 can't just BlockMove the data directly.  Instead we have to
  181.                 move one row from our source and then advance the pointer of the
  182.                 destination to the next row
  183.             */
  184.             for (  i = 0; i < WIDTHHEIGHT; i++ ) { 
  185.                 BlockMove(tp,p,(WIDTHHEIGHT / BYTESIZE) * depth);        /* move a row */
  186.                 /*
  187.                   now advance the destination pointer to the next row.  Don't
  188.                   forget to and off he high bit which marks this as a pixelmap
  189.                  */
  190.                 p += ((**(GgwPtr->portPixMap)).rowBytes ) & 0x7fff;
  191.                 tp += (WIDTHHEIGHT / BYTESIZE) * depth; /*move the source pointer to the next row */
  192.             }
  193.             HUnlock(h);
  194.             ReleaseResource(h);
  195.         }        
  196. }
  197.  
  198. void DrawIcResources()
  199. {
  200.     Rect    r1;
  201.     
  202.     
  203.         
  204.     
  205.     r1 = PlotRect;
  206.     MakeANewGWorld(&r1);
  207.         
  208.     if ( GgwPtr ) {
  209.         r1 = (**(GgwPtr->portPixMap)).bounds;
  210.         r1.right = r1.left + 16;
  211.         CopyBits((BitMap *)(*(GgwPtr->portPixMap)),&bullseyeWindow->portBits,
  212.                     &(r1),&PlotRect,srcCopy,NULL);
  213.     }
  214. }
  215.         
  216.  
  217. /*****
  218.  * DrawBullseye()
  219.  *
  220.  *    Draws the bullseye.
  221.  *
  222.  *****/
  223.  
  224. void DrawBullseye(short    active)
  225. {
  226.     #pragma unused(active)
  227.     SetPort(bullseyeWindow);
  228.     EraseRect(&bullseyeWindow->portRect);
  229.     
  230.     
  231.     
  232.     DrawIcResources();
  233.  
  234. }
  235. /* end DrawBullseye */